/**   
 * @Title: RechargeUninBinding.java 
 * @Package com.esun.elk.ui.views.userinfo.recharge 
 * @Description: 银联充值页
 * @author Miko baoz@500wan.com   
 * @date 2011-5-25 下午03:02:03  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.recharg;

import com.esun.config.Config;
import com.esun.config.Language;
import com.esun.model.UserInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.UserRechargeInfoCollectCommunicater;
import com.esun.rabbit2.communicaters.recharg.RechargeController;
import com.esun.rabbit2.communicaters.recharg.RechargeUserInfo;
import com.esun.ui.R;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @ClassName: RechargeUninBinding
 * @Description: 银联充值页
 * @author Miko baoz@500wan.com
 * @date 2011-5-25 下午03:02:03
 * 
 */
public class RechargeUninBinding extends Activity implements OnClickListener, OnTouchListener {

	private ImageButton btnGoBack;
	private Button btnRecharge;

	private EditText etMoney;
	private EditText etPhoneNum;

	private Handler handler;

	private final int RECHARGE_SUCCESS = 100000;
	private final int RECHARGE_FAILED = 999999;
	private final int USERRECHARGEINFOUPLOAD_SUCCEED = 10001;
	private final int USERRECHARGEINFOUPLOAD_FAIL = 10002;

	private ProgressDialog progress;

	private TextView tvCardNum;

	private String strPrice;
	private String strPhoneNum;

	private RelativeLayout relativeLayoutMain;
	private InputMethodManager imm;
	private LinearLayout linearLayoutMain;

	private RechargeController controller = new RechargeController();

	/** 2011-08-08 city add for uninbinding set */
	public static boolean isRechargeSucceed = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recharge_unin);

		initPage();
		initHandler();

	}

	private void initPage() {

		btnGoBack = (ImageButton) this.findViewById(R.id.recharge_unin_btn_goBack);
		btnGoBack.setOnClickListener(this);
		btnRecharge = (Button) this.findViewById(R.id.recharge_unin_btn_submit);
		btnRecharge.setOnClickListener(this);

		etMoney = (EditText) this.findViewById(R.id.recharge_unin_et_price);
		etPhoneNum = (EditText) this.findViewById(R.id.recharge_unin_et_phoneNum);

		tvCardNum = (TextView) this.findViewById(R.id.recharge_unin_tv_cardNum);

		StringBuilder strShowNum = new StringBuilder();
		String[] strCardNum = RechargeUserInfo.getUserInfo().getCardNum().split("|");
		EDebug.o(RechargeUninBinding.this, "card num is " + RechargeUserInfo.getUserInfo().getCardNum());
		strShowNum.append("绑定卡号：");
		for (int i = 0; i < strCardNum.length; i++) {
			if (i > 4 && i < strCardNum.length - 4) {
				strShowNum.append("*");
			} else {
				strShowNum.append(strCardNum[i]);
			}
		}
		tvCardNum.setText(strShowNum.toString());

		progress = ViewerUtil.showProgressDialog(this, "处理中...");

		relativeLayoutMain = (RelativeLayout) findViewById(R.id.rechargeunin_relative_main);
		relativeLayoutMain.setOnTouchListener(this);
		linearLayoutMain = (LinearLayout) findViewById(R.id.rechargeunin_linear_main);
		linearLayoutMain.setOnTouchListener(this);
		imm = (InputMethodManager) this.getSystemService(INPUT_METHOD_SERVICE);
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.cancel();

				switch (msg.what) {

				case RECHARGE_SUCCESS:

					RechargeMenuPage.isRechargeSucceed = true;
					UserInfo.getUserInfo().setRechargeSucceed(true);
					if (UserInfo.getUserInfo().isBuyingRecharge()) {
						ViewerUtil.showBuyingRechargeDialog(RechargeUninBinding.this, RechargeUserInfo.getUserInfo().getCardNum());
					} else {
						// ViewerUtil.showTipDialog(RechargeUninBinding.this,
						// Language.RECHARGETIP_SHENGZHOUXING_SUCCEED, true);
						isRechargeSucceed = true;
						RechargeUninBinding.this.finish();
					}

					/** 2011-09-19 by city 用户充值信息统计 */
					doRechargeInfoUpload();
					break;

				case RECHARGE_FAILED:

					ViewerUtil.showTipDialog(RechargeUninBinding.this, controller.getMSG(), false);

					break;

				case USERRECHARGEINFOUPLOAD_SUCCEED:
					EDebug.o(this, "充值信息上传成功");
					break;

				case USERRECHARGEINFOUPLOAD_FAIL:
					EDebug.o(this, "充值信息上传失败");
					break;

				case Config.CONNECTION_FAIL:
					progress.cancel();
					EDebug.o(this, "通讯错误");
          Toast.makeText(RechargeUninBinding.this, Language.STR_CONNECTIONFAIL.toString(), Toast.LENGTH_SHORT).show();
					//ViewerUtil.showTipDialog(RechargeUninBinding.this, Language.STR_CONNECTIONFAIL, false);
					break;

				}
			}
		};
	}

	private void doRecharge(final String strPrice, final String strPhoneNum) {
		new Thread() {

			@Override
			public void run() {
				super.run();

				try {
					if (controller.isYlRechargeOk(strPhoneNum, strPrice)) {
						Message msg = handler.obtainMessage(RECHARGE_SUCCESS);
						handler.sendMessage(msg);
					} else {
						Message msg = handler.obtainMessage(RECHARGE_FAILED);
						handler.sendMessage(msg);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Message msg = handler.obtainMessage(Config.CONNECTION_FAIL);
					handler.sendMessage(msg);
				}

			}
		}.start();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.recharge_unin_btn_goBack:

			finish();

			break;

		case R.id.recharge_unin_btn_submit:
			strPrice = etMoney.getText().toString();
			strPhoneNum = etPhoneNum.getText().toString();

			if (strPrice.length() < 1) {
				ViewerUtil.showTipDialog(this, "请输入金额", false);
			} else if (strPhoneNum.length() < 1) {
				ViewerUtil.showTipDialog(this, "请输入手机号码", false);
			} else {
				progress.show();
				doRecharge(strPrice, strPhoneNum);
			}
			break;
		}

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		imm.hideSoftInputFromWindow(etMoney.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(etPhoneNum.getWindowToken(), 0);
		return false;
	}

	/** 2011-09-19 by city 用户充值信息统计 */
	private void doRechargeInfoUpload() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					UserRechargeInfoCollectCommunicater userRechargeInfoCollectCommunicater = new UserRechargeInfoCollectCommunicater();
					if (userRechargeInfoCollectCommunicater.isGetInfoSucceed(strPrice, 1, RechargeUninBinding.this.getPackageManager()
							.getPackageInfo(Config.APP_PACKAGE_INFO, 0).versionName)) {
						Message msg = handler.obtainMessage(USERRECHARGEINFOUPLOAD_SUCCEED, null);
						handler.sendMessage(msg);
					} else {
						Message msg = handler.obtainMessage(USERRECHARGEINFOUPLOAD_FAIL, null);
						handler.sendMessage(msg);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Message msg = handler.obtainMessage(Config.CONNECTION_FAIL, null);
					handler.sendMessage(msg);
				}
			}
		}.start();
	}

}
